package com.yf.modules.security.model.dto;

import com.yf.domain.security.Menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author chentianwei
 * @date 2023/5/23 16:38
 * @description menuDto
 */
@Getter
@Setter
@EqualsAndHashCode
public class MenuDto implements Serializable {

    @Schema(name = "主键")
    private Long id;

    @Schema(name = "子节点")
    private List<MenuDto> children;

    @Schema(name = "类型")
    private Integer type;

    @Schema(name = "菜单权限")
    private String permission;

    @Schema(name = "菜单标题")
    private String title;

    @Schema(name = "菜单排序")
    private Integer menuSort;

    @Schema(name = "菜单路径")
    private String path;

    @Schema(name = "组件名称")
    private String component;

    @Schema(name = "Pid")
    private Long pid;

    @Schema(name = "菜单总数")
    private Integer subCount;

    @Schema(name = "是否是外链链接")
    private Boolean iFrame;

    @Schema(name = "是否要缓存")
    private Boolean cache;

    @Schema(name = "是否隐藏")
    private Boolean hidden;

    @Schema(name = "组件名称")
    private String componentName;

    @Schema(name = "图标")
    private String icon;

    @Schema(name = "是否有子节点")
    private Boolean hasChildren;

    public Boolean getHasChildren() {
        return subCount > 0;
    }

    public Boolean getLeaf() {
        return subCount <= 0;
    }

    public String getLabel() {
        return title;
    }

    public static MenuDto menuEntityToMenuDto(Menu menu) {

        MenuDto dto = new MenuDto();
        dto.setId(menu.getId());
        dto.setCache(menu.getCache());
        dto.setMenuSort(menu.getMenuSort());
        dto.setIcon(menu.getIcon());
        dto.setComponent(menu.getComponent());
        dto.setHidden(menu.getHidden());
        dto.setComponentName(menu.getComponentName());
        dto.setPath(menu.getPath());
        dto.setIFrame(menu.getIFrame());
        dto.setPid(menu.getPid());
        dto.setTitle(menu.getTitle());
        dto.setType(menu.getType());
        dto.setPermission(menu.getPermission());
        dto.setSubCount(menu.getSubCount());
        dto.setHasChildren(menu.getHasChildren());

        return dto;
    }

    public static List<MenuDto> menuListToMenuDtoList(Collection<Menu> menus) {

        List<MenuDto> menuDtoList = new ArrayList<>();
        for (Menu menu : menus) {
            MenuDto dto = menuEntityToMenuDto(menu);
            menuDtoList.add(dto);
        }
        return menuDtoList;
    }
}
